//掌柜号
var Seller = [];

app.controller('sellerTaskCtrl', ['$scope', function ($scope) {
	var needNewPagination = true;
	var endDate = Utils.nowDate();
	var startDate = Utils.dateAdd(endDate, -30);
	var proId = "";
	
	var sellerNick = "";
	getSellers();		//获取掌柜号
	
	//获取掌柜号
	function getSellers() {
		AQ.callAQ("getSellers.do", null, function(result) {
			Seller = [];
			$("#sel-seller").empty();
			//$("#sel-seller").append(new Option("选择掌柜号", 0));
			var topOption = document.createElement("OPTION");
			topOption.text = "选择掌柜号";
			topOption.value = "0";
			$("#sel-seller")[0].options.add(topOption);
			for (var i = 1; i < result.length; i++) {
				Seller.push(result[i]);
				//var newOption = new Option(result[i].sellerNick, result[i].sellerNick);
				//$("#sel-seller").append(newOption);
				var newOption = document.createElement("OPTION");
	            newOption.text = result[i].sellerNick;
	            newOption.value = result[i].sellerNick;
	            $("#sel-seller")[0].options.add(newOption);
			}
			
			sellerNick = Utils.getUrlParam("sellerNick");
			if($.trim(sellerNick) == "" && result.length >= 2 && result[1] != null) {
				sellerNick = result[1].sellerNick;
			}
			$scope.sellerNick = sellerNick;
			refreshSellerTask(1);
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	/* 刷新掌柜号列表 */
	function refreshSellerTask(pageNo) {
		var param = {
			sellerNick: sellerNick,
			startTime: startDate,
			endTime: endDate,
			pageNo: pageNo,
			pageCount: 15,
			proId: proId,
		};
		AQ.callAQJSON("getSellerTask.do", param, function(result) {
			var list = (result.data)[0];
			var regionsObj = (result.data)[1];
			var ipCountObj = (result.data)[2];
			
			var idReg = /id=([0-9]{8,20})/;
			var data = [];
			for (var i=0; i<list.length; i++) {
				var proIdArr = idReg.exec(list[i].productLink);
				var productId = proIdArr != null ? proIdArr[1] : "—";
				var region = list[i].region.length > 2 ? list[i].region.substr(0, 2) : list[i].region;
				var city = list[i].city.length > 3 ? list[i].city.substr(0, 3) : list[i].city;
				var isp = list[i].isp.length > 3 ? "" : list[i].isp;
				data.push({
					productId: productId,
					pruductLink: list[i].pruductLink,
					taskId: list[i].taskId,
					publishTime: list[i].publishTime,
					payTime: list[i].payTime,
					buyerNick: list[i].buyerNick,
					totalMoney: "￥ "+Utils.formatMoney(list[i].totalMoney),
					ip: list[i].ip,
					address: region+city+isp,
				});
			}
			$scope.$apply(function () {
				$scope.listData = data;
				if(pageNo == 1 && needNewPagination) {	//创建分页
					//生成统计表格
					$scope.regionsObj = regionsObj;
					$scope.ipCountObj = ipCountObj;
					var num_entries = result.size;
					Utils.newPagination("Pagination", num_entries, function(page_index, jq) {
						if(page_index == 0 && needNewPagination) {
							needNewPagination = false;
							return;
						}
						refreshSellerTask(page_index+1);
					}, 15);
				}
			});
			chartInit();
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	/**
	 * 选择掌柜
	 */
	$("#sel-seller").change(function() {
		var value = $("#sel-seller").val();
		if(value != 0) {
			sellerNick = value;
			$scope.sellerNick = sellerNick;
			needNewPagination = true;
			refreshSellerTask(1);
		}
    });
	
	/**
	 * 下载报表
	 */
	$("#down-task-form").click(function() {
		if(sellerNick == null) {
			Utils.msgDialog("请先选择一个掌柜，再下载报表！");
			return;
		}
		var param = "sellerNick="+sellerNick+"&startTime="+startDate+"&endTime="+endDate+"&proId="+proId;
		document.location = "../feizuan-web/" + "exportSellerTaskForm.do?" + param;
	});
	
	/* 选择日期 */
	$("#sel-date").click(function() {
		startDate = $("#the-month").val();
		endDate = Utils.dateAdd(startDate, 30);
		$("#count-date-tips").text("统计30天的数据："+ startDate +"到"+ endDate);	
		needNewPagination = true;
		refreshSellerTask(1);
    });
	
	/* 确定：id筛选 */
	$(".filter-by-id").find(".bt").click(function() {
		var inputVal = $.trim($(".filter-by-id").find(".txt").val());
		var idReg = /id=([0-9]{8,20})/;
		var proIdArr = idReg.exec("id="+inputVal);
		if(inputVal != "" && proIdArr == null) {
			Utils.msgDialog("请输入正确的宝贝链接或宝贝id！");
			return;
		}
		proId = proIdArr[1];
		needNewPagination = true;
		refreshSellerTask(1);
    });
	
	
	//初始化饼状图
	function chartInit() {
		gvChartInit();
			
		$('#ip-area-table').gvChart({
			chartType: 'PieChart',
			gvSettings: {
				vAxis: {title: 'No of players'},
				hAxis: {title: 'Month'},
				width: 500,
				height: 265
			}
		});	
		$("#ip-repeat-count").gvChart({
			chartType: 'PieChart',
			gvSettings: {
				vAxis: {title: 'No of players'},
				hAxis: {title: 'Month'},
				width: 500,
				height: 265
			}
		});
	}
	
	
}]);



$(document).ready(function(){
		
	var endDate = Utils.nowDate();
	var startDate = Utils.dateAdd(endDate, -30);
	$("#count-date-tips").text("统计30天的数据："+ startDate +"到"+ endDate);
	$("#the-month").val(startDate);
	
	$(".filter-by-id").hover(function() {
		$(".filter-by-id").find(".bt").show();
    }, function() {
		$(".filter-by-id").find(".bt").hide();
	});
	
});
